﻿namespace Mockingbird.MVC.Helpers
{
    using System.Web;
    using System.Web.Mvc;
    using Core.Utilities.Cryptography;

    public static class GravatarHelper
    {

        public static HtmlString Gravatar(this HtmlHelper helper, string email, int size, string styleClass = "gravatar")
        {
            var result = "<img src='{0}' alt='Gravatar' class='{1}' />";
            var url = GetGravatarURL(email, size);
            return new HtmlString(string.Format(result, url, styleClass));
        }



        public static string GravatarURL(this HtmlHelper helper, string email, int size)
        {
            return GetGravatarURL(email, size);
        }

        private static string GetGravatarURL(string email, int size)
        {
            return (string.Format("http://www.gravatar.com/avatar/{0}?s={1}&r=PG&d=identicon",
                                  Cryptography.GetMd5Hash(email), size.ToString()));
        }

        private static string GetGravatarURL(string email, int size, string defaultImagePath)
        {
            return GetGravatarURL(email, size) + string.Format("&default={0}",
                                                               defaultImagePath);
        }
    }
}